package com.tnl.app.service.impl;

import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Repository;

import com.tnl.app.dao.CompanyDAO;
import com.tnl.app.dao.EmployerDAO;
import com.tnl.app.dao.SchPersonDAO;
import com.tnl.app.entity.Company;
import com.tnl.app.entity.Employer;
import com.tnl.app.entity.SchJob;
import com.tnl.app.entity.SchJobPerson;
import com.tnl.app.entity.SchPerson;

@Repository
public class SchPersonService {
	
	@Autowired
	private SchPersonDAO schPersonDAO;
	@Autowired
	private CompanyDAO companyDAO;
	@Autowired
	private SchJobService schJobService;
	@Autowired
	private EmployerDAO employerDAO;

	public void insert(SchPerson schPerson) {
		// TODO Auto-generated method stub
//		schPersonDAO.insert(schPerson);
	}

	public SchPerson getById(String id) {
		// TODO Auto-generated method stub
		return schPersonDAO.getById(id);
	}

	public void update(SchPerson schPerson) {
		// TODO Auto-generated method stub
//		schPersonDAO.update(schPerson);
	}

	public SchPerson getByUnionid(String unionid) {
		// TODO Auto-generated method stub
		return schPersonDAO.getByUnionid(unionid);
	}

	public SchJobPerson hasSendEmail(String unionid, String jobId) {
		// TODO Auto-generated method stub
		return schPersonDAO.hasSendEmail(unionid,jobId);
	}

	public Boolean addSendRecord(String unionid, String jobId) {
		// TODO Auto-generated method stub
		SchJob job = schJobService.getById(jobId);
//		if (job !=null) {
//			Employer e =employerDAO.getById(Integer.valueOf(job.getCreateBy()));
//			if (e !=null) {
//				com.tnl.utils.MailUtils.sendAttachmentMail(e.getEmail(), "候选人投递通知", getTemplateContext(unionid,job),"/usr/share/tomcat8/uploads/");
//			}
//		}
		return schPersonDAO.addSendRecord(unionid,jobId)==1? true:false;
	}

	private String getTemplateContext(String unionid, SchJob job) {
		// TODO Auto-generated method stub
		StringBuilder sBuilder = new StringBuilder("<html><body>");
		
		
		
		sBuilder.append("</body></html>");
		return sBuilder.toString();
	}

	public String addBase(SchPerson schPerson) {
		// TODO Auto-generated method stub
		
		return schPersonDAO.addBase(schPerson);
	}

	public void updateBase(SchPerson schPerson) {
		// TODO Auto-generated method stub
		schPersonDAO.updateBase(schPerson);
	}

	public void updateExcept(SchPerson schPerson) {
		// TODO Auto-generated method stub
//		Company c1 = companyDAO.getByName(schPerson.getExceptCompanyName1());
//		if (c1 == null) {
//			schPerson.setExceptCompany1((int) ((Math.random() * 9 + 1) * Math.pow(10, 5 - 1)));
//		} else {
//			schPerson.setExceptCompany1(c1.getId());
//		}
//		Company c2 = companyDAO.getByName(schPerson.getExceptCompanyName1());
//		if (c2 == null) {
//			schPerson.setExceptCompany2((int) ((Math.random() * 9 + 1) * Math.pow(10, 5 - 1)));
//		} else {
//			schPerson.setExceptCompany2(c2.getId());
//		}
//		Company c3 = companyDAO.getByName(schPerson.getExceptCompanyName1());
//		if (c3 == null) {
//			schPerson.setExceptCompany3((int) ((Math.random() * 9 + 1) * Math.pow(10, 5 - 1)));
//		} else {
//			schPerson.setExceptCompany3(c3.getId());
//		}
		schPersonDAO.updateExcept(schPerson);
	}

}
